#!/bin/bash

# TDM, tbk display manager, or tiny display manager,
# is a session selector after login.
# It links the starting script to default and start
# the startx script.
VERSION=0.4.0

# directory settings
CONFDIR="$HOME/.tdm"
SESSIONS=${CONFDIR}/sessions
EXTRA=${CONFDIR}/extra
SAVELAST=1

nulltype(){
	type "$1" > /dev/null 2>/dev/null
}

fallback(){
	if [[ -n $1 ]]; then
		echo -e "\033[31;1m$1\033[0m"
	fi
	exec $SHELL
}

get_needs(){
	local _type=$(file -p --mime-type "$1" | awk '{print $2}' | cut -d'/' -f1)
	if [ ${_type} == "text" ]; then
		grep '#[ \t]*needs[ \t]*:' "$1"|sed -e 's/#.*://g'
	fi
}

valid_item(){
	local needs
	needs=($(get_needs "$1"))
	for _i in ${needs[@]}
	do
		nulltype ${_i} || return 1
	done
	return 0
}

# started from startx, so start session
if [[ -n $1 && $1 = "--xstart" ]]; then
	if [[ -f "${CONFDIR}/tdmexit" ]]; then
		. "${CONFDIR}/tdmexit"
	fi
	if [[ -x "/tmp/tdmdefault" ]]; then
		exec /tmp/tdmdefault
	else
		exec "${CONFDIR}/default"
	fi
fi

clear
# check for a 'good' tty
(basename $(tty)|grep -q tty) || fallback "Invalid tty"

# X started, exit
pgrep X>/dev/null&&fallback 'X started.'

# build confdir
if [ ! -d "${CONFDIR}" ]; then
	tdmctl init
fi

# otherwise, run as the session chosen script
if [[ -f "${CONFDIR}/tdminit" ]]; then
	source "${CONFDIR}/tdminit"
fi

# XID: X session Number
# TOTAL: Number of items
# sid : id that user selects

let XID=0
let TOTAL=0
xsessions=()
prglist=()
DEFOPT=""

if [[ -x "${CONFDIR}/default" ]]; then
	SDEFAULT=$(readlink "${CONFDIR}/default")
	DEFAULTWM=$(basename ${SDEFAULT})
else
	SDEFAULT=
	DEFAULTWM=
fi

if [ -d ${SESSIONS} ]; then
	for script in "${SESSIONS}"/*; do
		if [ -x "${script}" ] && valid_item "${script}" ; then
			xsessions[$XID]="${script}"
			NAME=$(basename ${script})
			prglist=(${prglist[@]} ${XID} ${NAME})
			if [ "${NAME}" == ${DEFAULTWM} ]; then
				DEFOPT="--default-item ${XID}"
			fi
			let XID=$(($XID+1))
			let TOTAL=$(($TOTAL+1))
		fi
	done
else
	echo "${SESSIONS} doesn't exist."
	echo "making this directory."
	mkdir -p ${SESSIONS}
fi

if [ -d ${EXTRA} ]; then
	for script in "${EXTRA}"/*; do
		if [ -x "${script}" ] && valid_item "${script}" ; then
			xsessions[$TOTAL]="${script}"
			NAME="extra/$(basename ${script})"
			prglist=(${prglist[@]} ${TOTAL} ${NAME})
			let TOTAL=$(($TOTAL+1))
		fi
	done
fi

if [ $TOTAL -eq 0 ]; then
	fallback "No sessions found."
fi

tdm_curses(){
	sid=$(dialog --stdout ${DEFOPT} --menu "TDM ${VERSION}" 0 0 0 ${prglist[@]})
	[ -n "$sid" ]||fallback "Falling back to shell."
}

tdm_text(){
	echo "This is TDM ${VERSION}, a tiny display manager."
	echo "Please select from the following: (default ${DEFAULTWM})"

	local _i=0
	while [ ${_i} -lt ${TOTAL} ]
	do
		echo ${_i} ${prglist[${_i}*2+1]}
		let _i=${_i}+1
	done

	echo -n "Program ID: "
	read sid
}

if ! nulltype dialog; then
#no dialog program, force to use tdm_text
	TDMUI=tdm_text
elif [ ! "${TDMUI}" == "tdm_text" ]; then
	TDMUI=tdm_curses
fi
${TDMUI}

rm -f /tmp/tdmdefault
if [[ (-n $sid) && ($sid -lt $TOTAL) && ($sid -ge $XID) ]]; then
	exec ${xsessions[$sid]}
elif [[ (-n $sid) && ($sid -lt $XID) && ($sid -ge 0) ]]; then
	if [[ ${SAVELAST} -ne 0 ]]; then
		ln -sf ${xsessions[${sid}]} "${CONFDIR}/default"
	else
		ln -sf ${xsessions[${sid}]} "/tmp/tdmdefault"
	fi
	startx
	logout
else
	echo "Unknown value,load default."
	if [ -x "${CONFDIR}/default" ]; then
		startx
		logout
	else
		fallback "Session not defined,fallback."
	fi
fi

